package com.huilan.hgt.util;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.springframework.web.multipart.MultipartFile;

/**
 *
 * @author llp
 */
public class ImageUtil {

    /**
     * 保存图片到服务器
     * @param imageFile
     * @return 返回图片名称
     */
    public static String readImageFile(MultipartFile imageFile){
        String path = SystemConfigUtil.getConfig("IMAGE_PATH");
        String date = DateUtil.getStringDateShort();

        String os = System.getProperty("os.name").toLowerCase();
        if(os.contains("mac")){
            path = "/Users/ghy/Desktop/image";
        }
        // 文件夹
        String dir = path ;

        String imageName = imageFile.getOriginalFilename();
        //图片后缀名
        String imagePrefix = imageName.substring(imageName.lastIndexOf("."));
        String newImageFileName = date + File.separator + System.currentTimeMillis() + imagePrefix;

        //图片文件路径
        String imageFilePath = dir + newImageFileName;

        File image = new File(imageFilePath);

        File fileParent = image.getParentFile();
        if(!fileParent.exists()  && !fileParent.isDirectory()){
            fileParent.mkdirs();
        }
        try {
            imageFile.transferTo(image);
        } catch (IOException e) {
            e.printStackTrace();
            newImageFileName = "文件保存出错!";
        }
        return newImageFileName;
    }

}
